package com.modong.jianjian.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.metaapp.demo.R
import com.metaapp.demo.custom.IStatusView
import com.metaapp.demo.custom.LoadStatusViewHolder

/**
 * Created by CLEI on 2018/7/10.
 * Description :
 */
abstract class BaseFragment<T : IBasePresenter> : Fragment(), IBaseView {

    protected var mPresenter: T? = null
    private lateinit var mHolder: LoadStatusViewHolder
    protected lateinit var mContext: Context

    /**
     * 设置视图id
     */
    protected abstract val layoutId: Int

    /**
     * 设置自定义加载状态视图
     */
    protected val customLoadStatusView: IStatusView?
        get() = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = FrameLayout(mContext)
        val view = inflater.inflate(layoutId, rootView, false)
        rootView.addView(view)
        addStatusView(rootView)
        return rootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        setListener()
        mPresenter = createPresenter()
        mPresenter?.attachView(this)//初始化数据
        mPresenter?.start()
        initData()
        setTitle()
    }

    /**
     * 创建模块对应的Presenter
     */
    protected abstract fun createPresenter(): T

    /**
     * 添加loading视图
     */
    private fun addStatusView(container: ViewGroup) {
        mHolder = LoadStatusViewHolder(mContext)
        val iPlaceView = customLoadStatusView
        iPlaceView?.let {
            mHolder.setCustomStatusView(it)
        }
        val statusView = mHolder.get()
        val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        params.topMargin = titleBarHeight.toInt()
        statusView.layoutParams = params
        container.addView(mHolder.get())
    }

    /**
     * 获取标题栏高度（用于设置加载状态视图topMargin）
     */
    protected open val titleBarHeight: Float
        get() = 0f

    protected open fun initView() {

    }

    /**
     * 设置view监听
     */
    protected open fun setListener() {

    }

    protected open fun initData() {

    }

    /**
     * 设置页面标题
     */
    protected open fun setTitle() {

    }

    /**
     * 显示/隐藏加载视图
     */
    override fun setLoadingView(isShow: Boolean) {
        if (isShow) {
            val loadingView = mHolder.showLoadingView()
            setLoadingView(loadingView)
        } else {
            mHolder.hideLoadingView()
        }
    }

    /**
     * 重写此方法修改loading内容
     */
    protected open fun setLoadingView(loadingView: View) {

    }

    /**
     * 显示/隐藏加载失败视图
     */
    override fun setLoadFailedView(isShow: Boolean) {
        if (isShow) {
            val emptyView = mHolder.showEmptyPlaceView()
            setLoadFailedView(emptyView)
            emptyView.setOnClickListener { onClickLoadFailedView() }
        } else {
            mHolder.hideEmptyPlaceView()
        }
    }

    /**
     * 重写此方法加载失败的视图
     */
    protected fun setLoadFailedView(failedView: View) {
        val ivError = failedView.findViewById<ImageView>(R.id.iv_empty_icon)
        val tvError = failedView.findViewById<TextView>(R.id.tv_empty_tips)
        ivError.setImageResource(R.drawable.ic_empty)
        tvError.text = getString(R.string.tips_load_fail)

    }

    /**
     * 显示/隐藏空数据视图
     */
    override fun setNoDataView(isShow: Boolean) {
        if (isShow) {
            val emptyView = mHolder.showEmptyPlaceView()
            setNoDataView(emptyView)
        } else {
            mHolder.hideEmptyPlaceView()
        }
    }

    /**
     * 重写此方法设置空数据时视图
     */
    protected fun setNoDataView(emptyView: View) {

    }

    /**
     * 点击加载失败页面处理
     */
    protected open fun onClickLoadFailedView() {

    }

    override fun longToast(content: String) {
        Toast.makeText(mContext, content, Toast.LENGTH_LONG).show()
    }

    override fun shortToast(content: String) {
        Toast.makeText(mContext, content, Toast.LENGTH_SHORT).show()
    }

    override fun onUserTokenError() {
        (activity as BaseActivity).onUserTokenError()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mPresenter?.detachView()
    }
}
